package L7.L710;

import java.util.Arrays;
import java.util.List;

/**
 * 找数组中两个出现奇数次的数字,分组异或
 *
 * @author: luolidong
 * @create: 2021-07-10 19:18
 * @version: 1.0
 */
public class Demo2 {
    public int[] get(int[] nums) {
        //1.先计算两个数异或的值
        int a = 0;
        for (int n : nums) {
            a ^= n;
        }
        //&,依次与上1,10,100...从而判断哪位是1
        int b = 1;
        while (0 == (b & a)) {
            b <<= 1;//左移，直到确定哪位是1
        }
        //再遍历异或，通过&b是否为1分组
        int c0 = 0;
        int c1 = 0;
        for (int n : nums) {
            if ((n & b) == 0) {
                c0 ^= n;
            } else {
                c1 ^= n;
            }
        }
        //c0,c1即为答案
        return new int[]{c0, c1};
    }

    public static void main(String[] args) {
        Demo2 demo2 = new Demo2();
        int[] ints = demo2.get(new int[]{1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9});
        System.out.println(Arrays.toString(ints));

    }
}
